<template>
  <Header></Header>
    <div class="fixed">
      <div class="bgColor">
        <h1 class="main-shopcart">购物车</h1>
      </div>
      <div class="container">
        <div class="main">
          <div class="nav">
            <span class="left">全部商品</span>
          </div>
          <ul class="head">
            <li class="item check">
              <el-checkbox v-model="isChecked"
              @change="checkAll"
              >全选</el-checkbox>
            </li>
            <li class="item classInfo">商品信息</li>
            <li class="item price">单价</li>
            <li class="item count">
              数量
            </li>
            <li class="item total">金额</li>
            <li class="item function">操作</li>
          </ul>
          <div v-if="true">
            <ul class="haveorder"
            v-for='(item,index) in cartList'
              :key="item.goods_id" 
            >
              <li class="order-item">
                <!-- //单选 -->
                <el-checkbox v-model="item.check"
                @change="cartStore.itemChecked(index)"
                ></el-checkbox>
              </li>
              <li class="order-item info" >
                <div class="courseimg" >
                  <img :src="item.img_small_logo" alt="">
                </div>
                <div class="course-name"><p>{{ item.title }}</p></div>
              </li>
<!-- //{{ item.cart_number}} -->
              <li class="order-item">￥{{ item.price }}</li>
              <li class="order-item num">
                <el-input-number v-model="item.cart_number"  :min="1" :max="10" @change="handleChange(item.goods_id,item.cart_number)" /></li>
              <li class="order-item totoalprice">￥{{ item.price* item.cart_number}}</li>
              <li class="order-item delete" @click="del(item.goods_id)">
             <el-button type="danger" round>删除</el-button>
              </li>
            </ul>
          </div>
          <div class="noOrder" v-else>
            <img src="/image/norder365.png" alt="">
            <div class="order-alert">哎呦！暂无订单</div>
          </div>
          <el-divider class="line"></el-divider>
          <ul class="foot">
            <li><el-button  type="primary" @click="deletecar">清空购物车</el-button></li>
            <li class="foot-item">已选商品<span class="unique">{{ total.number }}</span></li>
            <li class="foot-item">合计<span class="unique">{{ total.price }}</span></li>
            <li >
              
              <el-button  size="large" type="danger" @click="goOrder">去结算</el-button>
            </li>
          </ul>
        </div>
      </div>
    </div>
  <Foot></Foot>
</template>

<script setup>
// import { ref } from 'vue'
import {useRouter} from 'vue-router';
// import { inject } from 'vue';
import store from '../../store';
import { ElMessage, ElMessageBox } from 'element-plus'
import { onBeforeMount,ref } from 'vue';
import { storeToRefs } from 'pinia'
// pinia
import{useCartStore} from '../../store/cart'
// import axios from 'axios'
import {getShopCarList,deleteShopCar,clearallCar,updatenumber} from '../../api/cart'
const cartStore = useCartStore();
const {cartList, isChecked,total} = storeToRefs(cartStore);

// const renovate = inject('reload');

// 生命周期
     onBeforeMount(() => {
     getShopCarList(store.state.userInfo.id).then(res => {
 cartStore.addCart(res.cart);
       console.log(res.cart);
    //    if (window.location.href.indexOf('#reloaded') == -1) {
    //     window.location.href = window.location.href + '#reloaded';
    //     window.location.reload();
    // }

    

})

})
// 复选框点击选择

const checkAll = ()=>{

if(isChecked.value){

 // 不选(true)
 cartStore.unAll();

}
else{
 // 全选(false)
 cartStore.All();

}
}


  
// 删除购物车数据
const del = (goods_id)=>{
  // console.log(typeof(goods_id))
  // console.log('999',store.state.userInfo.id)
  // console.log('444',goods_id);
    ElMessageBox.confirm('确定删除该商品吗？', '删除', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(( ) => {
      // console.log('444',goods_id);
      
        deleteShopCar(store.state.userInfo.id,goods_id).then(res=>{
          console.log('555',res);
        }) 
        
      ElMessage.success({
        message: '删除成功!'
      });
      // 成功后再次请求
      getShopCarList(store.state.userInfo.id).then(res => {
        cartStore.cartList = res.cart || [];
        // renovate();
        // 刷新页面
        location.reload()
      })
        
    }).catch(() => {
      ElMessage.info({
        message: '已取消删除'
      });
    });
} 


// 删除购物车数据
const deletecar = ()=>{
    ElMessageBox.confirm('确定删除该商品吗？', '删除', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(( ) => {
      clearallCar(store.state.userInfo.id).then(res=>{
          console.log('555',res);
        }) 
        
      ElMessage.success({
        message: '删除成功!'
      });
      // 成功后再次请求
      getShopCarList(store.state.userInfo.id).then(res => {
        cartStore.cartList = res.cart || [];
        
        // location.reload()
      })
        
    }).catch(() => {
      ElMessage.info({
        message: '已取消删除'
      });
    });
} 


const nums = ref(1)



// 商品数量增加减少
const handleChange = (goods_id,cart_number) => {
  // cartStore.cartList.forEach(v=>{
  //    console.log('001',v.cart_number);
   // }) 
  updatenumber(store.state.userInfo.id,goods_id,cart_number).then(res=>{
          console.log('000',res);
        })
       
  
}


// 去结算页面
let router = useRouter();
const goOrder = ()=>{
   router.push('/ConfirmView');
}
</script>
<style scoped>
.fixed{
  width: 100%;
  height: 100%;
  background: #FFFFFF;
}
.bgColor{
  width: 100%;
  height: 200px;
  background-color: red;
  background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
  background-size: 400% 400%;
  animation: gradient 15s ease infinite;
}
@keyframes gradient {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}
.container{
  width: 1200px;
  margin: -100px auto 50px auto;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0px 2px 5px #a7a6a6;
}
.main{
  padding:20px;
  border-radius: 5px;
}
.main-shop{
  position: relative;
  display: flex;
  align-content: center;
}
.main-shop i{
  font-size: 35px;
  padding: 20px 10px 0 0;
  color: #FF4400;
  font-weight: bold;
}
.main-shopcart{
  
  width: 1200px;
  margin: 0 auto;
  height: 42px;
  font-size: 24px;
  font-family: Microsoft YaHei;
  font-weight: bold;
  line-height: 35px;
  color: #FFFFFF;
  /* padding: 30px 0; */
  padding-left: 30px;
  padding-top: 30px;
  opacity: 1;
}
.nav{
  display: flex;
  justify-content: space-between;
  border-bottom: 2px solid #E6E6E6;

}
.nav .left{
  width: 80px;
  height: 26px;
  font-size: 18px;
  font-family: Microsoft YaHei;
  font-weight: bold;
  line-height: 0px;
  color: #FF4400;
  opacity: 1;
  border-bottom: 2px solid #FF4400;
}
.nav .right{
  width: 108px;
  height: 24px;
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  line-height: 0px;
  color: #333333;
  opacity: 0.5;
}

/* 头部开始 */
.head{
  display: flex;
  padding: 0 10px;
  margin: 20px 0;
  width: 100%;
  height: 35px;
  line-height: 35px;
  background: #FCFCFC;
  opacity: 1;
  border-radius: 0px;
  box-sizing: border-box;
  border-radius: 5px;
  box-shadow: 0px 2px 5px 2px #cccccc;
}
.head .item{
  width: 150px;
  font-size: 14px;
  color: #333333;
}
.check .all{
  margin-right: 10px;
}
.check .text{
  width: 1487px;
  height: 40px;

}
.classInfo{
  width: 400px!important;
  color: #333333;
}
/* 头部结束 */

/* 订单开始 */
.haveorder{
  display: flex;
  /* width: 100%; */
  height: 200px;
  background: #FCFCFC;
  margin-bottom: 10px;
  border-radius: 5px;
  box-shadow: 0px 2px 5px 2px #cccccc;
  
}
.haveorder .order-item{
  height: 180px;
  line-height: 150px;
  /* overflow: hidden; */
  margin:  5px;
}
.order-item:first-child{
  width: 30px;
}
.order-item{
  width: 150px;
  font-size: 5px;
  
  color: #333333;
}
.totoalprice{
  color: #E2231A;
}
.num{
  width: 190px !important;
  padding-left: 15px;
}
.info{
  display: flex;
  width: 470px !important;
  height: 200px;
  line-height: 200px;
}
.courseimg{
  margin:40px 20px 40px 0;
  width: 150px;
  height: 120px;
}
.courseimg img{
  width: 90%;
  height: 100%;
}
.info .course-name p{
  width: 200px;
  height: 200px;
   /* word-wrap:break-word;  */
   white-space:nowrap;
 
  overflow: hidden;
  text-overflow: ellipsis;
}

.delete{
  cursor: pointer;
}
.deletd-text{
  margin-left: 5px;
}
ul li {
  list-style: none;
}
/* 订单结束 */

/* 暂无订单开始 */
.noOrder{
  width: 100%;
  height: 100%;
  text-align: center;
  margin:200px 0;
}
.order-alert{
  height: 31px;
  font-size: 20px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  line-height: 0px;
  color: #B5B9BC;
  opacity: 1;
  margin:20px 120px;

}
/* 暂无订单结束 */
.foot{
  display: flex;
  justify-content: flex-end;
  width: 100%;
  height: 40px;
  line-height: 40px;
  color: #333333;
  margin-bottom: 10px;
}
.foot li:nth-child(1){
  margin-right: 30px;
}
.foot li:nth-child(4){
  margin-right: 80px;
}
.foot-item{
  width: 200px;
  height: 40px;
  line-height: 40px;
  font-size: 14px;
  font-weight: 400;
  color: #333333;
}
.unique{
  margin-left: 5px;
  font-size: 24px;
  color: #FF4400;
}
.el-input-n{
  float: left;
  margin-top:60px;
  margin-left: -30px;
}
</style>